const app = getApp();
let aaa = wx.createInnerAudioContext();
Page({
  data: {
    current_id: 0,
    img: '',
    openid: '',
    token: '',
    page:1,
    total:0,
    cardCur: 0,
    moduleCooperId:0,
    moduleCooperIndex:0,
    inputTop:0,
    inputHeight:0,
    blindBoxList: [],
    swiperList: [],
    boxList:[],
    indexShow:true,
    searchShow:false,
    inputValue: '',
    searchList:[],
    swiperLists: [
      {
        id: 0,
        url: '../../images/nav.png'
      }, {
        id: 1,
        url: '../../icon/bar.png',
      }
    ],//广告轮播 已经注释了
  },

  onLoad: function () {
    //获取微信小程序 胶囊位置
    let obj = wx.getMenuButtonBoundingClientRect()
    // console.log('ddfd',obj)
    this.setData({
      inputTop:obj.top,
      inputHeight:obj.height
    })
  },

  onShow: function () {

    let that = this;
    var token = wx.getStorageSync('token');
    var img = wx.getStorageSync('userInfo')
    if (!token) {
      wx.navigateTo({
        url: '../auth/auth',
      })
      return;
    }
    this.setData({
      token: token,
      img: img.wxInfo && img.wxInfo.avatarUrl
    })

    this.getInfo();
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.getInfo();
    this.getTyptlist();
    wx.showLoading({
      title: '刷新中...',
      duration: 2000
    })
    setTimeout(function () {
      wx.stopPullDownRefresh();  //停止下拉刷新动画
    }, 1000);
    
  },
  //分享
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },	
  // 上拉加载
	onReachBottom: function() {
    var that = this;
    this.data.page = this.data.page+1
      //显示加载动画
      wx.showLoading({
        title:"刷新中",
        mask:true
      })

      app.util.request({
        url: app.do('index/typeList'),
        method: 'Post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          typeId: that.data.moduleCooperId,
          page:that.data.page
        },
        success: function (res) {
          wx.hideLoading()//关闭加载动画
          if (res.data.code == "0000") {
            let dataList = res.data.data.typeList;
            if(dataList.length>0){
            //  es6解构
              that.data.boxList=[...that.data.boxList,...dataList]
              that.setData({
                boxList : that.data.boxList
              })
            }else{

            }

          } else if (res.data.code == "2001") {
            wx.navigateTo({
              url: '../auth/auth',
            })
          }
   
        },
        fail: function (res) {
          wx.hideLoading({})
          var text = res.data.message;
          wx.showToast({
            title: text,
            icon: 'none',
            duration: 2000,
          })
        }
      })
			

	},
  //获取首页数据
  getInfo() { 
    var that = this;
    app.util.request({
      url: app.do('index/index'),
      method: 'Post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: that.data.token
      },
      success: function (res) {
        if (res.data.code == "0000") {
          that.setData({
            swiperList: res.data.data.homeData,
            blindBoxList: res.data.data.boxType,
          })
          that.data.moduleCooperId = res.data.data.boxType[0].id
          that.getTyptlist(that.data.moduleCooperId,that.data.page)
         
        } else if (res.data.code == "2001") {
          wx.navigateTo({
            url: '../auth/auth',
          })
        }
 
      },
      fail: function (res) {
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  getTyptlist(typeId,page){
    var that = this;
    app.util.request({
      url: app.do('index/typeList'),
      method: 'Post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        typeId: typeId,
        page:1
      },
      success: function (res) {
        if (res.data.code == "0000") {
          that.setData({
            boxList: res.data.data.typeList,
            total: res.data.data.total
          })
        } else if (res.data.code == "2001") {
          wx.navigateTo({
            url: '../auth/auth',
          })
        }
 
      },
      fail: function (res) {
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },

  cardSwiper(e) {
  	this.cardCur = e.detail.current
  },
  // 切换盲盒分类
  toggle(e){
      this.data.page = 1;
    
      var that = this
      this.setData({
        moduleCooperIndex: e.currentTarget.dataset.index,
        moduleCooperId: e.currentTarget.dataset.id
      })
      this.getTyptlist(e.currentTarget.dataset.id,this.data.page)
  },
  //跳转盲盒系列详情
  clickChoose(e){
    // console.log('dddd',e.currentTarget.dataset )
    wx.navigateTo({
      url: '../choose/choose?id='+e.currentTarget.dataset.id
    })
  },
  //搜索
  inputClick(e){
    this.setData({
      inputValue : e.detail.value
    })
    console.log('input',this.data.inputValue)
    if(!this.data.inputValue==''){
      this.setData({
        indexShow: false,
        searchShow: true
      })
      var that = this;
      app.util.request({
        url: app.do('index/indexSearch'),
        method: 'Post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          boxname: this.data.inputValue,
        },
        success: function (res) {
          if (res.data.code == "0000") {
            that.setData({
              searchList: res.data.data.boxList,
            })
          } 
   
        },
        fail: function (res) {
          var text = res.data.message;
          wx.showToast({
            title: text,
            icon: 'none',
            duration: 2000,
          })
        }
      })
    }
    else{
      this.setData({
        indexShow: true,
        searchShow: false
      })
      this.getTyptlist(this.data.moduleCooperId,this.data.page)
    }
  }
  
})